package com.zhss.eshop.schedule.stock;


import com.zhss.eshop.order.domain.dto.OrderInfoDTO;
import com.zhss.eshop.order.domain.dto.OrderItemDTO;
import com.zhss.eshop.order.service.OrderService;
import com.zhss.eshop.schedule.domain.SaleDeliveryScheduleResult;
import com.zhss.eshop.schedule.domain.dto.ScheduleOrderPickingItemDTO;
import com.zhss.eshop.schedule.domain.model.ScheduleGoodsAllocationStock;
import com.zhss.eshop.schedule.domain.model.ScheduleGoodsAllocationStockDetail;
import com.zhss.eshop.schedule.domain.model.ScheduleGoodsStock;
import com.zhss.eshop.schedule.mapper.ScheduleGoodsAllocationStockDetailMapper;
import com.zhss.eshop.schedule.mapper.ScheduleGoodsAllocationStockMapper;
import com.zhss.eshop.schedule.mapper.ScheduleGoodsStockMapper;
import com.zhss.eshop.schedule.service.SaleDeliveryScheduler;
import com.zhss.eshop.wms.domain.dto.WmsGoodsAllocationStockDetailDTO;
import com.zhss.eshop.wms.domain.dto.WmsReturnGoodsInputOrderDTO;
import com.zhss.eshop.wms.domain.dto.WmsReturnGoodsInputOrderItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 退货入库
 * @author suyuan
 *
 */
@Component
@Scope("prototype")
public class ReturnGoodsInputScheduleStockUpdater extends AbstractScheduleStockUpdater {

	/**
	 * 退货入库单
	 */
	private WmsReturnGoodsInputOrderDTO returnGoodsInputOrder;

	/**
	 * 商品库存管理的DAO组件
	 */
	@Autowired
	private ScheduleGoodsStockMapper goodsStockDAO;
	/**
	 * 货位库存管理的DAO组件
	 */
	@Autowired
	private ScheduleGoodsAllocationStockMapper goodsAllocationStockDAO;
	/**
	 * 货位库存明细管理的DAO组件
	 */
	@Autowired
	private ScheduleGoodsAllocationStockDetailMapper stockDetailDAO;
	/**
	 * 销售出库调度器
	 */
	@Autowired
	private SaleDeliveryScheduler saleDeliveryScheduler;
	/**
	 * 订单中心接口
	 */
	@Autowired
	private OrderService orderService;

	/**
	 * 更新商品库存
	 */
	@Override
	protected void updateGoodsStock() throws Exception {
		List<WmsReturnGoodsInputOrderItemDTO> returnGoodsInputOrderItems =
				returnGoodsInputOrder.getItems();
		for(WmsReturnGoodsInputOrderItemDTO returnGoodsInputOrderItem : returnGoodsInputOrderItems) {
			ScheduleGoodsStock goodsStock = goodsStockDAO.getBySkuId(
					returnGoodsInputOrderItem.getGoodsSkuId());
			goodsStock.setAvailableStockQuantity(goodsStock.getAvailableStockQuantity()
					+ returnGoodsInputOrderItem.getArrivalCount());
			goodsStock.setOutputStockQuantity(goodsStock.getOutputStockQuantity()
					- returnGoodsInputOrderItem.getArrivalCount());
			goodsStockDAO.updateByPrimaryKeySelective(goodsStock);
		}
	}

	/**
	 * 更新货位库存
	 */
	@Override
	protected void updateGoodsAllocationStock() throws Exception {
		List<WmsReturnGoodsInputOrderItemDTO> items = returnGoodsInputOrder.getItems();

		OrderInfoDTO order = orderService.getOrderById(returnGoodsInputOrder.getOrderId());

		for(WmsReturnGoodsInputOrderItemDTO item : items) {
			OrderItemDTO targetOrderItem = null;

			for(OrderItemDTO orderItem : order.getOrderItems()) {
				if(orderItem.getGoodsSkuId().equals(item.getGoodsSkuId())) {
					targetOrderItem = orderItem;
					break;
				}
			}

			SaleDeliveryScheduleResult scheduleResult = saleDeliveryScheduler.getScheduleResult(targetOrderItem);

			for(ScheduleOrderPickingItemDTO pickingItem : scheduleResult.getPickingItems()) {
				ScheduleGoodsAllocationStock goodsAllocationStock = goodsAllocationStockDAO.getBySkuId(
						pickingItem.getGoodsAllocationId(), pickingItem.getGoodsSkuId());
				goodsAllocationStock.setAvailableStockQuantity(goodsAllocationStock.getAvailableStockQuantity()
						+ pickingItem.getPickingCount());
				goodsAllocationStock.setOutputStockQuantity(goodsAllocationStock.getOutputStockQuantity()
						- pickingItem.getPickingCount());
				goodsAllocationStockDAO.updateByPrimaryKeySelective(goodsAllocationStock);
			}
		}
	}

	/**
	 * 更新货位库存明细
	 */
	@Override
	protected void updateGoodsAllocationStockDetail() throws Exception {
		List<WmsReturnGoodsInputOrderItemDTO> items = returnGoodsInputOrder.getItems();

		for(WmsReturnGoodsInputOrderItemDTO item : items) {
			List<WmsGoodsAllocationStockDetailDTO> stockDetails = item.getStockDetails();
			for(WmsGoodsAllocationStockDetailDTO stockDetail : stockDetails) {
				stockDetailDAO.insertSelective(stockDetail.clone(ScheduleGoodsAllocationStockDetail.class));
			}
		}
 	}

	/**
	 * 设置需要的参数
	 */
	@Override
	public void setParameter(Object parameter) {
		this.returnGoodsInputOrder = (WmsReturnGoodsInputOrderDTO) parameter;
	}

}
